home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / Apple Game Sprockets / Examples / SoundSprocketTest / TS3Utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-16  |  11.1 KB  |  469 lines  |  [TEXT/CWIE]

  1. /*
  2.  *    File:        TS3Utils.c
  3.  *    Author:        Dan Venolia
  4.  *
  5.  *    Contents:    Some utilities.
  6.  *
  7.  *    Copyright © 1996 Apple Computer, Inc.
  8.  */
  9.  
  10. #include <assert.h>
  11. #include <math.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14.  
  15. #include "TS3Utils.h"
  16.  
  17.  
  18. #ifndef HONOR_RANGE
  19.     #define HONOR_RANGE        0
  20. #endif
  21.  
  22.  
  23. static UserItemUPP            gUtilsOKUserItemProc    = NULL;
  24.  
  25.  
  26. static pascal void Utils_OKUserItem(
  27.     DialogPtr            inDialog,
  28.     short                inItem);
  29.  
  30.  
  31. /* =============================================================================
  32.  *        Utils_Init (external)
  33.  *
  34.  *    Initializes the utilities.
  35.  * ========================================================================== */
  36. void Utils_Init(
  37.     void)
  38. {
  39. }
  40.  
  41.  
  42. /* =============================================================================
  43.  *        Utils_Exit (external)
  44.  *
  45.  *    Prepares for exit.
  46.  * ========================================================================== */
  47. void Utils_Exit(
  48.     void)
  49. {
  50.     if (gUtilsOKUserItemProc != NULL)
  51.     {
  52.         DisposeRoutineDescriptor(gUtilsOKUserItemProc);
  53.         gUtilsOKUserItemProc = NULL;
  54.     }
  55. }
  56.  
  57.  
  58. /* =============================================================================
  59.  *        Utils_Interval (external)
  60.  *
  61.  *    Returns the interval, in seconds, between two results of Microseconds.
  62.  * ========================================================================== */
  63. float Utils_Interval(
  64.     const UnsignedWide*    inPrevTime,
  65.     const UnsignedWide*    inCurrTime)
  66. {
  67.     //• Should actually take high words into consideration too
  68.     return 0.000001*(inCurrTime->lo - inPrevTime->lo);
  69. }
  70.  
  71.  
  72. /* =============================================================================
  73.  *        Utils_OKUserItem (internal)
  74.  *
  75.  *    Draws the user item used for framing the OK button.  The user item should
  76.  *    be four pixels larger than the OK button in each direction.
  77.  * ========================================================================== */
  78. pascal void Utils_OKUserItem(
  79.     DialogPtr            inDialog,
  80.     short                inItem)
  81. {
  82.     short                itemType;
  83.     Handle                itemHandle;
  84.     Rect                itemBounds;
  85.     
  86.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  87.     
  88.     PenSize(3, 3);
  89.     FrameRoundRect(&itemBounds, 16, 16);
  90.     PenSize(1, 1);
  91. }
  92.  
  93.  
  94. /* =============================================================================
  95.  *        Utils_GetOKUserItemProc (external)
  96.  *
  97.  *    Returns the UPP for the dialog user item that outlines the default button.
  98.  * ========================================================================== */
  99. UserItemUPP Utils_GetOKUserItemProc(
  100.     void)
  101. {
  102.     if (gUtilsOKUserItemProc == NULL)
  103.     {
  104.         gUtilsOKUserItemProc = NewUserItemProc(Utils_OKUserItem);
  105.         assert(gUtilsOKUserItemProc != NULL);
  106.     }
  107.     
  108.     return gUtilsOKUserItemProc;
  109. }
  110.  
  111.  
  112. /* =============================================================================
  113.  *        Utils_SetStr255Field (external)
  114.  *
  115.  *    Changes the text of the given dialog item to the given string value.  If
  116.  *    inHasValue is false, then the field is emptied instead.  The dialog item must
  117.  *    of course be editable or static text.
  118.  * ========================================================================== */
  119. void Utils_SetStr255Field(
  120.     DialogPtr            inDialog,
  121.     short                inItem,
  122.     ConstStr255Param    inValue,
  123.     Boolean                inHasValue)
  124. {
  125.     short                itemType;
  126.     Handle                itemHandle;
  127.     Rect                itemBounds;
  128.     
  129.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  130.     if (inHasValue)
  131.     {
  132.         SetDialogItemText(itemHandle, inValue);
  133.     }
  134.     else
  135.     {
  136.         SetDialogItemText(itemHandle, "\p");
  137.     }
  138. }
  139.  
  140.  
  141. /* =============================================================================
  142.  *        Utils_GetStr255Field (external)
  143.  *
  144.  *    Interprets the value of the given editable or static text field as an
  145.  *    string value.  If outHasValue is NULL then the field must not be blank.
  146.  *    If outHasValue is non-NULL then it is set to false if the field is blank
  147.  *    and true if it has a value.  Returns false if there is a formatting problem.
  148.  * ========================================================================== */
  149. Boolean Utils_GetStr255Field(
  150.     DialogPtr            inDialog,
  151.     short                inItem,
  152.     Str255                outValue,
  153.     Boolean*            outHasValue)
  154. {
  155.     short                itemType;
  156.     Handle                itemHandle;
  157.     Rect                itemBounds;
  158.     
  159.     // Grab the string
  160.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  161.     GetDialogItemText(itemHandle, outValue);
  162.     
  163.     if (outHasValue != NULL)
  164.     {
  165.         *outHasValue = outValue[0] > 0;
  166.     }
  167.     
  168.     return true;
  169. }
  170.  
  171.  
  172. /* =============================================================================
  173.  *        Utils_SetUInt32Field (external)
  174.  *
  175.  *    Changes the text of the given dialog item to the given integer value.  If
  176.  *    inHasValue is false, then the field is emptied instead.  The dialog item must
  177.  *    of course be editable or static text.
  178.  * ========================================================================== */
  179. void Utils_SetUInt32Field(
  180.     DialogPtr            inDialog,
  181.     short                inItem,
  182.     UInt32                inValue,
  183.     Boolean                inHasValue)
  184. {
  185.     Str255                str;
  186.     short                itemType;
  187.     Handle                itemHandle;
  188.     Rect                itemBounds;
  189.     
  190.     if (inHasValue)
  191.     {
  192.         sprintf((char*) str, "x%lu", inValue);
  193.         str[0] = strlen((char*) str) - 1;
  194.     }
  195.     else
  196.     {
  197.         str[0] = 0;
  198.     }
  199.     
  200.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  201.     SetDialogItemText(itemHandle, str);
  202. }
  203.  
  204.  
  205. /* =============================================================================
  206.  *        Utils_GetUInt32Field (external)
  207.  *
  208.  *    Interprets the value of the given editable or static text field as an
  209.  *    integer value.  If outHasValue is NULL then the field must not be blank.
  210.  *    If outHasValue is non-NULL then it is set to false if the field is blank
  211.  *    and true if it has a value.  Returns false if there is a formatting problem.
  212.  * ========================================================================== */
  213. Boolean Utils_GetUInt32Field(
  214.     DialogPtr            inDialog,
  215.     short                inItem,
  216.     UInt32*                outValue,
  217.     Boolean*            outHasValue,
  218.     UInt32                inMin,
  219.     UInt32                inMax)
  220. {
  221.     Str255                str;
  222.     short                itemType;
  223.     Handle                itemHandle;
  224.     Rect                itemBounds;
  225.     
  226.     // Grab the string
  227.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  228.     GetDialogItemText(itemHandle, str);
  229.     str[str[0]+1] = 0;
  230.     
  231.     // Parse it
  232.     if (sscanf((char*) str, "%*c%lu", outValue) > 0)
  233.     {
  234.         // Got a number
  235.         if (outHasValue != NULL)
  236.         {
  237.             *outHasValue = true;
  238.         }
  239.         
  240.         // Check the range
  241.         #if HONOR_RANGE
  242.             if (*outValue < inMin || *outValue > inMax)
  243.             {
  244.                 return false;
  245.             }
  246.         #endif
  247.     }
  248.     else
  249.     {
  250.         if (outHasValue != NULL)
  251.         {
  252.             *outHasValue = false;
  253.         }
  254.         else
  255.         {
  256.             return false;
  257.         }
  258.     }
  259.     
  260.     return true;
  261. }
  262.  
  263.  
  264. /* =============================================================================
  265.  *        Utils_SetFloatField (external)
  266.  *
  267.  *    Changes the text of the given dialog item to the given float value.  If
  268.  *    inHasValue is false, then the field is emptied instead.  The dialog item must
  269.  *    of course be editable or static text.
  270.  * ========================================================================== */
  271. void Utils_SetFloatField(
  272.     DialogPtr            inDialog,
  273.     short                inItem,
  274.     float                inValue,
  275.     Boolean                inHasValue)
  276. {
  277.     Str255                str;
  278.     short                itemType;
  279.     Handle                itemHandle;
  280.     Rect                itemBounds;
  281.     float                abs;
  282.     float                fract;
  283.     
  284.     if (inHasValue)
  285.     {
  286.         abs = fabsf(inValue);
  287.         fract = abs-floorf(abs);
  288.         if (fract < 0.0001 || fract > 0.9999)
  289.         {
  290.             // Value is close enough to integer to show it that way
  291.             sprintf((char*) str, "x%.0f", inValue);
  292.         }
  293.         else
  294.         {
  295.             // Has a fractional part
  296.             sprintf((char*) str, "x%.2f", inValue);
  297.         }
  298.         str[0] = strlen((char*) str) - 1;
  299.     }
  300.     else
  301.     {
  302.         str[0] = 0;
  303.     }
  304.     
  305.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  306.     SetDialogItemText(itemHandle, str);
  307. }
  308.  
  309.  
  310. /* =============================================================================
  311.  *        Utils_GetFloatField (external)
  312.  *
  313.  *    Interprets the value of the given editable or static text field as an
  314.  *    float value.  If outHasValue is NULL then the field must not be blank.
  315.  *    If outHasValue is non-NULL then it is set to false if the field is blank
  316.  *    and true if it has a value.  Returns false if there is a formatting problem.
  317.  * ========================================================================== */
  318. Boolean Utils_GetFloatField(
  319.     DialogPtr            inDialog,
  320.     short                inItem,
  321.     float*                outValue,
  322.     Boolean*            outHasValue,
  323.     float                inMin,
  324.     float                inMax)
  325. {
  326.     Str255                str;
  327.     short                itemType;
  328.     Handle                itemHandle;
  329.     Rect                itemBounds;
  330.     
  331.     // Grab the string
  332.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  333.     GetDialogItemText(itemHandle, str);
  334.     str[str[0]+1] = 0;
  335.     
  336.     // Parse it
  337.     if (sscanf((char*) str, "%*c%f", outValue) > 0)
  338.     {
  339.         // Got a number
  340.         if (outHasValue != NULL)
  341.         {
  342.             *outHasValue = true;
  343.         }
  344.         
  345.         // Check the range
  346.         #if HONOR_RANGE
  347.             if (*outValue < inMin || *outValue > inMax)
  348.             {
  349.                 return false;
  350.             }
  351.         #endif
  352.     }
  353.     else
  354.     {
  355.         if (outHasValue != NULL)
  356.         {
  357.             *outHasValue = false;
  358.         }
  359.         else
  360.         {
  361.             return false;
  362.         }
  363.     }
  364.     
  365.     return true;
  366. }
  367.  
  368.  
  369. /* =============================================================================
  370.  *        Utils_SetVector3DField (external)
  371.  *
  372.  *    Changes the text of the given dialog item to the given TQ3ector3D value.  If
  373.  *    inHasValue is false, then the field is emptied instead.  The dialog item must
  374.  *    of course be editable or static text.
  375.  * ========================================================================== */
  376. void Utils_SetVector3DField(
  377.     DialogPtr            inDialog,
  378.     short                inItem,
  379.     const TQ3Vector3D*    inValue,
  380.     Boolean                inHasValue)
  381. {
  382.     Str255                str;
  383.     short                itemType;
  384.     Handle                itemHandle;
  385.     Rect                itemBounds;
  386.     float                abs;
  387.     float                fract;
  388.     UInt32                xPrecision;
  389.     UInt32                yPrecision;
  390.     UInt32                zPrecision;
  391.     
  392.     if (inHasValue)
  393.     {
  394.         abs = fabsf(inValue->x);
  395.         fract = abs-floorf(abs);
  396.         xPrecision = (fract < 0.0001 || fract > 0.9999) ? 0 : 2;
  397.         
  398.         abs = fabsf(inValue->y);
  399.         fract = abs-floorf(abs);
  400.         yPrecision = (fract < 0.0001 || fract > 0.9999) ? 0 : 2;
  401.         
  402.         abs = fabsf(inValue->z);
  403.         fract = abs-floorf(abs);
  404.         zPrecision = (fract < 0.0001 || fract > 0.9999) ? 0 : 2;
  405.         
  406.         sprintf((char*) str, "x%.*f %.*f %.*f", xPrecision, inValue->x, yPrecision, inValue->y, zPrecision, inValue->z);
  407.         str[0] = strlen((char*) str) - 1;
  408.     }
  409.     else
  410.     {
  411.         str[0] = 0;
  412.     }
  413.     
  414.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  415.     SetDialogItemText(itemHandle, str);
  416. }
  417.  
  418.  
  419. /* =============================================================================
  420.  *        Utils_GetVector3DField (external)
  421.  *
  422.  *    Interprets the value of the given editable or static text field as an
  423.  *    TQ3Vector3D value.  If outHasValue is NULL then the field must not be blank.
  424.  *    If outHasValue is non-NULL then it is set to false if the field is blank
  425.  *    and true if it has a value.  Returns false if there is a formatting problem.
  426.  * ========================================================================== */
  427. Boolean Utils_GetVector3DField(
  428.     DialogPtr            inDialog,
  429.     short                inItem,
  430.     TQ3Vector3D*        outValue,
  431.     Boolean*            outHasValue)
  432. {
  433.     Str255                str;
  434.     short                itemType;
  435.     Handle                itemHandle;
  436.     Rect                itemBounds;
  437.     
  438.     // Grab the string
  439.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  440.     GetDialogItemText(itemHandle, str);
  441.     str[str[0]+1] = 0;
  442.     
  443.     // Parse it
  444.     if (sscanf((char*) str, "%*c%f%f%f", &outValue->x, &outValue->y, &outValue->z) == 3)
  445.     {
  446.         // Got a number
  447.         if (outHasValue != NULL)
  448.         {
  449.             *outHasValue = true;
  450.         }
  451.     }
  452.     else
  453.     {
  454.         if (outHasValue != NULL)
  455.         {
  456.             *outHasValue = false;
  457.         }
  458.         else
  459.         {
  460.             return false;
  461.         }
  462.     }
  463.     
  464.     return true;
  465. }
  466.  
  467.  
  468.  
  469.